package com.example.wifi_scan;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 通过GET方式向服务器发送数据
 *
 * @author jph
 * Date:2014.09.27
 */
public class SendDateToServer {
    public static final int GatherData = 1;
    public static final int Location = 2;
    public static final int Test = 3;
    //private final String remostHost = "localhost";
    private final String remoteHost = "120.27.237.68";


    //数据采集URL
    private final String gatherURL = "http://" + remoteHost + ":8080/web/gatherData";
    //定位发送URL
    private final String locationURL = "http://" + remoteHost + ":8080/web/myLocation";
    //测试URL
    private final String testURL = "http://" + remoteHost + ":8080/webtest/testServlet";

    public static final int SEND_SUCCESS = 0x123;
    public static final int SEND_FAIL = 0x124;
    private Handler handler;

    public SendDateToServer(Handler handler) {
        this.handler = handler;
    }


    /**
     * 发送Get请求给服务器，利用okhttp
     * @param rssi 要发送的RSSI
     * @param state 功能选择:
     *              gatherData 收集数据
     *              Location 定位
     *              Test 测试
     */
    public void getAsyn(RSSI rssi, int state) {
        String url = new String();
        switch (state) {
            //采集状态码 1
            case GatherData:
                url = gatherURL + "?data=" + rssi.toString();
                break;
            //定位状态码 2
            case Location:
                url = locationURL + "?data=" + rssi.toString();
                break;
            //测试状态码 3
            case Test:
                url = testURL + "?data=" + rssi.toString();
                break;
            default:
                Log.d("myerror", "状态错误");
        }
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            //失败
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("myresp","发送成功");
            }

            //成功的回调函数
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Log.d("myresp","发送成功");
                }
            }
        });
    }


}


/*
*
number:3  APRSSI:  6c:b7:49:55:81:85,-24 70:f9:6d:2b:80:20,-45 70:f9:6d:2b:80:30,-64


number:3  APRSSI:  6c:b7:49:55:81:85:-24 70:f9:6d:2b:80:20:-45 70:f9:6d:2b:80:30:-64


number:3  APRSSI:  6c:b7:49:55:81:85:-24 70:f9:6d:2b:80:20:-46 70:f9:6d:2b:80:30:-61
* */